package com.wd.tech.view.fragment;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.wd.tech.R;
import com.wd.tech.bean.BannerBean;
import com.wd.tech.bean.ZixunBean;
import com.wd.tech.mvp.base.BaseFragment;
import com.wd.tech.mvp.base.BasePresenter;
import com.wd.tech.mvp.presenter.PresenterImpl;
import com.wd.tech.mvp.util.MyUrl;
import com.wd.tech.view.activity.PlateActivity;
import com.wd.tech.view.activity.SousuoActivity;
import com.wd.tech.view.activity.Zixun_XQ;
import com.wd.tech.view.adapter.ZixunAdapter;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ZiXun extends BaseFragment {


    @BindView(R.id.iv_plate)
    ImageView ivPlate;
    private Banner banner;

    List<ZixunBean.ResultBean> list1 = new ArrayList<>();
    ArrayList<String> list = new ArrayList<>();

    private RecyclerView rv;
    private ImageView iv_search;


    @Override
    protected void startCoding() {
        mPresenter.startgetInfo(MyUrl.BASE_ZiXun_LunBo, BannerBean.class);


        HashMap<String, Object> map = new HashMap<>();
        map.put("plateId", 54);
        map.put("page", 1);
        map.put("count", 15);
        mPresenter.startgetInofHava(MyUrl.BASE_ZiXun_LunBo, ZixunBean.class, map);


        mPresenter.startgetInfo(MyUrl.BASE_Zixun_recyc, BannerBean.class);


        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("page", 1);
        map1.put("count", 10);
        map1.put("plateId", 1);
        mPresenter.startgetInofHava(MyUrl.BASE_Zixun_recyc, ZixunBean.class, map1);

        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getContext(), SousuoActivity.class);

                startActivity(intent);
            }
        });

    }

    @Override
    protected BasePresenter initPresenter() {
        return new PresenterImpl();
    }

    @Override
    protected void initView(View view) {
        banner = view.findViewById(R.id.banner);
        rv = view.findViewById(R.id.rv);

        iv_search = view.findViewById(R.id.iv_search);


        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(RecyclerView.VERTICAL);
        rv.setLayoutManager(manager);


    }

    @Override
    protected int Layout() {
        return R.layout.fragment_zi_xun;
    }

    @Override
    public void onSuccess(Object o) {
        if (o instanceof BannerBean) {
            List<BannerBean.ResultBean> result = ((BannerBean) o).getResult();

            for (int i = 0; i < result.size(); i++) {
                list.add(result.get(i).getImageUrl());
            }


            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(getActivity()).load(path).apply(RequestOptions.bitmapTransform(new RoundedCorners(50))).into(imageView);
                }
            }).setDelayTime(1500).setImages(list).start();

        }


        if (o instanceof ZixunBean) {



            ZixunAdapter zixunAdapter = new ZixunAdapter(getContext(), ((ZixunBean) o).getResult());


            rv.setAdapter(zixunAdapter);


            zixunAdapter.setItemClick(new ZixunAdapter.ItemClick() {
                @Override
                public void setClick(int id) {
                    Intent intent = new Intent(getContext(), Zixun_XQ.class);

                    intent.putExtra("id",id);

                    startActivity(intent);
                }
            });


        }
    }

    @Override
    public void onError(String error) {

    }


    //跳转所有模块
    @OnClick(R.id.iv_plate)
    public void onViewClicked() {
        Intent intent=new Intent(getActivity(), PlateActivity.class);
        startActivity(intent);
    }
}
